﻿using Model.CommonEntities;
using Model.StrategyEntities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BLL.CalRevenue
{
    public class Cross
    {
        /// <summary>
        /// 简单交叉买入法
        /// </summary>
        /// <param name="TList">时间列表</param>
        /// <param name="LongList">长期线</param>
        /// <param name="ShortList">短期线</param>
        /// <param name="BenchmarkList">买入基准</param>
        /// <param name="CloseList">收盘价</param>
        /// <returns></returns>
        public static TotalResult SampleCross(List<decimal> TList, List<decimal> LongList, List<decimal> ShortList, List<decimal> BenchmarkList, List<decimal> CloseList)
        {
            //昨日交叉状态
            bool LastStatus = true;
            //今日交叉状态
            bool NowStatus = true;
            //首次买入价格
            decimal FirstPrice = 0;
            //买入价格
            decimal BuyPrice = decimal.MinValue;
            //买入日期
            string BuyData = string.Empty;
            //卖出价格
            decimal SellPrice = decimal.MinValue;
            //卖出时间
            string SellData = string.Empty;
            //盈利总和
            decimal Money = 0;
            //每次交易收益列表
            List<decimal> MyReturnList = new List<decimal>();
            //股价自然涨跌列表
            List<decimal> ObjectiveRetrunList = new List<decimal>();
            //交易详情
            string tradeinfo = string.Empty;

            for (int i = 0; i < TList.Count; i++)
            {
                //获取SB点,判断短期线上穿长期线
                if (LongList[i] < ShortList[i])
                {
                    NowStatus = true;
                }
                else if (LongList[i] > ShortList[i])
                {
                    NowStatus = false;
                }
                //如果昨天死叉且今天金叉，则买入，反之卖出
                //if (NowStatus == true && LastStatus == false)
                //{
                //    //忽略第一次交叉前的状态
                //    if (BuyPrice == decimal.MinValue)
                //    {
                //        FirstPrice = CloseList[i];
                //    }
                //    BuyPrice = BenchmarkList[i];
                //    BuyData = TList[i].ToString();
                //}
                //else if (NowStatus == false && LastStatus == true)
                //{
                //    SellPrice = BenchmarkList[i];
                //    if (BuyPrice != decimal.MinValue)
                //    {
                //        Money += SellPrice - BuyPrice;
                //        tradeinfo += "买入：" + BuyData + "/" + BuyPrice + "---卖出：" + TList[i] + "/" + SellPrice.ToString() + "---盈利：" + Money.ToString() + "\n";
                //    }
                //}

                //如果昨天死叉且今天金叉，则买入，反之卖出
                //由于初始状态是true，true，只有经历过一次死叉后，LastStatus才为false，所以当前if是经历过死叉后的完整金叉，而非残缺金叉，可进行交易。
                if (NowStatus == true && LastStatus == false)
                {
                    //第一次买入时记录价格，用于计算总收益
                    if (BuyPrice == decimal.MinValue)
                    {
                        FirstPrice = CloseList[i];
                    }
                    //如果今天金叉，昨天死叉
                    BuyPrice = BenchmarkList[i];
                    BuyData = TList[i].ToString();
                }
                else if (NowStatus == false && LastStatus == true)
                {
                    SellPrice = BenchmarkList[i];
                    //如果从来没有买过，死叉也不做交易，避免默认LastStatus=true带来的问题
                    if (BuyPrice != decimal.MinValue)
                    {
                        Money += SellPrice - BuyPrice;
                        tradeinfo += "买入：" + BuyData + "/" + BuyPrice + "---卖出：" + TList[i] + "/" + SellPrice.ToString() + "---盈利：" + Money.ToString() + "\r\n";
                    }
                }
                else
                {
                    //(true,true)可能是初始状态，也可能是金叉范围内
                    //(false,false)是死叉范围内
                    //两种情况均不作处理
                }

                //更新昨天交叉状态
                LastStatus = NowStatus;
                //记录累计收益
                if (BuyPrice == decimal.MinValue)
                {
                    MyReturnList.Add(CloseList[i]);
                }
                else
                {
                    MyReturnList.Add(FirstPrice + Money);
                }

                ObjectiveRetrunList.Add(CloseList[i]);

            }
            tradeinfo += "收益率：" + Math.Round((FirstPrice == 0 ? 0 : (Money / FirstPrice * 100)), 2) + "%\r\n";
            tradeinfo += "自然涨跌：" + Math.Round((ObjectiveRetrunList[ObjectiveRetrunList.Count - 1] - ObjectiveRetrunList[0]) / ObjectiveRetrunList[0] * 100, 2) + "%\r\n";
            
            var result = new TotalResult
            {
                TradeInfo = tradeinfo,
                TList = TList,
                StockPriseList = ObjectiveRetrunList,
                StockReturnList = MyReturnList,
                MyTotalReturn = Math.Round((FirstPrice == 0 ? 0 : (Money / FirstPrice * 100)), 2) + "%",
                ObjectiveTotalReturn = Math.Round((ObjectiveRetrunList[ObjectiveRetrunList.Count - 1] - ObjectiveRetrunList[0]) / ObjectiveRetrunList[0] * 100, 2) + "%"
            };
            return result;
        }
    }
}
